﻿<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../../../assets/docs.css" rel="stylesheet" type="text/css" media="screen" />
<title>MooTools Documentation - Utilities/Hash.Cookie</title>
</head>

<body>
  <div class="doc">
    <div class="methods">
			<h4 class="menu-item"><a href="#Hash-Cookie">Hash.Cookie</a></h4>
			<div class="menu-item"><a href="#"></a></div>
			<div class="menu-item"><a href="#Hash-Cookie:save">save</a></div>
			<div class="menu-item"><a href="#Hash-Cookie:load">load</a></div>
    </div>
    <div class="l-border r-border span-15 content">
				<h1 id="Hash-Cookie"><a href="#Hash-Cookie">Class: Hash.Cookie</a></h1>

<p class="description">Stores and loads a Hash as a Cookie using JSON format.</p>

<h3>Extends</h3>

<ul>
<li><a href="../Types/Hash.html#Hash">Hash</a></li>
</ul>
<h2><a href="#">Hash.Cookie Method: constructor</a></h2>

<h3 class="description">Syntax</h3>

<pre class="javascript"><span class="kw2">var</span> myHashCookie = <span class="kw2">new</span> Hash.<span class="me1">Cookie</span><span class="br0">(</span><span class="kw3">name</span><span class="br0">[</span>, options<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments</h3>

<ol>
<li>name    - (<em>string</em>) The key (name) for the cookie</li>
<li>options - (<em>object</em>) All of <a href="../../core/Utilities/Cookie.html">Cookie</a> options in addition an autoSave option.</li>
</ol>
<h4>Options</h4>

<ol>
<li>autoSave - (<em>boolean</em>: defaults to true) An option to save the cookie at every operation.</li>
</ol>
<h3>Returns</h3>

<ul>
<li>(<em>object</em>) A new Hash.Cookie instance.</li>
</ul>
<h3>Examples</h3>

<pre class="javascript"><span class="kw2">var</span> fruits = <span class="kw2">new</span> Hash.<span class="me1">Cookie</span><span class="br0">(</span><span class="st0">'myCookieName'</span>, <span class="br0">{</span>duration: <span class="nu0">3600</span><span class="br0">}</span><span class="br0">)</span>;
fruits.<span class="me1">extend</span><span class="br0">(</span><span class="br0">{</span>
    <span class="st0">'lemon'</span>: <span class="st0">'yellow'</span>,
    <span class="st0">'apple'</span>: <span class="st0">'red'</span>
<span class="br0">}</span><span class="br0">)</span>;
fruits.<span class="me1">set</span><span class="br0">(</span><span class="st0">'melon'</span>, <span class="st0">'green'</span><span class="br0">)</span>;
fruits.<span class="me1">get</span><span class="br0">(</span><span class="st0">'lemon'</span><span class="br0">)</span>; <span class="co1">// yellow</span>
&nbsp;
<span class="co1">// ... on another page ... values load automatically</span>
&nbsp;
<span class="kw2">var</span> fruits = <span class="kw2">new</span> Hash.<span class="me1">Cookie</span><span class="br0">(</span><span class="st0">'myCookieName'</span>, <span class="br0">{</span>duration: <span class="nu0">365</span><span class="br0">}</span><span class="br0">)</span>;
fruits.<span class="me1">get</span><span class="br0">(</span><span class="st0">'melon'</span><span class="br0">)</span>; <span class="co1">// green</span>
&nbsp;
fruits.<span class="me1">erase</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// delete cookie</span>
</pre>

<h3>Notes</h3>

<ul>
<li>All Hash methods are available in your Hash.Cookie instance. if autoSave options is set, every method call will result in your Cookie being saved.</li>
<li>Cookies have a limit of 4kb (4096 bytes). Therefore, be careful with your Hash size.</li>
<li>All Hash methods used on Hash.Cookie return the return value of the Hash method, unless you exceeded the Cookie size limit. In that case the result will be false.</li>
<li>If you plan to use large Cookies consider turning autoSave to off, and check the status of .save() everytime.</li>
<li>Creating a new instance automatically loads the data from the Cookie into the Hash. Cool Huh?</li>
</ul>
<h3>See Also</h3>

<ul>
<li><a href="../Types/Hash.html#Hash">Hash</a></li>
</ul>
<h2 id="Hash-Cookie:save"><a href="#Hash-Cookie:save">Hash.Cookie Method: save</a></h2>

<p class="description">Saves the Hash to the cookie. If the hash is empty, removes the cookie.</p>

<h3>Syntax</h3>

<pre class="javascript">myHashCookie.<span class="me1">save</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns</h3>

<ul>
<li>(<em>boolean</em>) Returns false when the JSON string cookie is too long (4kb), otherwise true.</li>
</ul>
<h3>Examples</h3>

<pre class="javascript"><span class="kw2">var</span> login = <span class="kw2">new</span> Hash.<span class="me1">Cookie</span><span class="br0">(</span><span class="st0">'userstatus'</span>, <span class="br0">{</span>autoSave: <span class="kw2">false</span><span class="br0">}</span><span class="br0">)</span>;
&nbsp;
login.<span class="me1">extend</span><span class="br0">(</span><span class="br0">{</span>
    <span class="st0">'username'</span>: <span class="st0">'John'</span>,
    <span class="st0">'credentials'</span>: <span class="br0">[</span><span class="nu0">4</span>, <span class="nu0">7</span>, <span class="nu0">9</span><span class="br0">]</span>
<span class="br0">}</span><span class="br0">)</span>;
login.<span class="me1">set</span><span class="br0">(</span><span class="st0">'last_message'</span>, <span class="st0">'User logged in!'</span><span class="br0">)</span>;
&nbsp;
login.<span class="me1">save</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// finally save the Hash</span>
</pre>

<h2 id="Hash-Cookie:load"><a href="#Hash-Cookie:load">Hash.Cookie Method: load</a></h2>

<p class="description">Loads the cookie and assigns it to the Hash.</p>

<h3>Syntax</h3>

<pre class="javascript">myHashCookie.<span class="me1">load</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns</h3>

<ul>
<li>(<em>object</em>) This Hash.Cookie instance.</li>
</ul>
<h3>Examples</h3>

<pre class="javascript"><span class="kw2">var</span> myHashCookie = <span class="kw2">new</span> Hash.<span class="me1">Cookie</span><span class="br0">(</span><span class="st0">'myCookie'</span><span class="br0">)</span>;
&nbsp;
<span class="br0">(</span><span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span><span class="br0">{</span>
    myHashCookie.<span class="me1">load</span><span class="br0">(</span><span class="br0">)</span>;
    <span class="kw1">if</span> <span class="br0">(</span>!myHashCookie.<span class="me1">getLength</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="kw3">alert</span><span class="br0">(</span><span class="st0">'Cookie Monster must of eaten it!'</span><span class="br0">)</span>;
<span class="br0">}</span><span class="br0">)</span>.<span class="me1">periodical</span><span class="br0">(</span><span class="nu0">5000</span><span class="br0">)</span>;
</pre>

<h3>Notes</h3>

<ul>
<li>Useful when polling.</li>
</ul>
    </div>
  </div>
<script type="text/javascript" src="../../../assets/mootools-core-1.3.js"></script>
<script type="text/javascript" src="../../../assets/mootools-more-smoothscroll.js"></script>
<script type="text/javascript" src="../../../assets/doc-assist.js"></script>
</body>
</html>
